﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Business;
using MyWeb.Data;

namespace MyWeb.TrangChinh
{
    public partial class Product : Page
    {
        private readonly ProductImgService _productImgService = new ProductImgService();
        private readonly ProductService _productService = new ProductService();
        private readonly StatisticsService _statisticsService = new StatisticsService();
        private readonly ProductSizeService _productSizeService=new ProductSizeService();
        private readonly SizeService _sizeService=new SizeService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["idProduct"];
                if (id != null)
                {
                    //tang view product
                    DataTable dtProduct = _productService.Product_GetById(id);
                    int distributionId = Int32.Parse(dtProduct.Rows[0]["Distribution_Id"].ToString());
                    if (Session["username"] != null)
                    {
                        int userId = Int32.Parse(Session["user_id"].ToString());
                        int viewNumber = _statisticsService.Statistics_GetView(userId, distributionId);
                        var statistics = new StatisticsInfo
                                             {
                                                 Distribution_Id = distributionId,
                                                 User_Id = userId,
                                                 ViewNumber = viewNumber + 1
                                             };
                        _statisticsService.Statistics_UpdateView(statistics);
                    }
                    DataTable dt = _productService.Product_GetById(id);
                    DataTable dtImgThumb = _productImgService.ProductImg_GetByProductId(id);
                    DataTable dtSize = _productSizeService.ProductSize_GetByProductId(id);
                    rptImgThums.DataSource = dtImgThumb;
                    rptImgThums.DataBind();
                    rptSize.DataSource = dtSize;
                    rptSize.DataBind();
                    imgHinhanh.ImageUrl = dt.Rows[0]["image"].ToString();
                    lblId.Text = dt.Rows[0]["Id"].ToString();
                    lblName.Text = dt.Rows[0]["name"].ToString().ToUpper();
                    lblPrice.Text = FormatPrice(dt.Rows[0]["price"].ToString());
                    lblPriceNew.Text = FormatPrice(dt.Rows[0]["pricenew"].ToString());
                    lblDescription.Text = dt.Rows[0]["detail"].ToString();
                }
            }
        }

        public string FormatPrice(string values)
        {
            values = values.Replace(",", "");
            values = values.Replace(".", "");
            string s = "";
            while (values.Length > 3)
            {
                s = "." + values.Substring(values.Length - 3) + s;
                values = values.Substring(0, values.Length - 3);
            }
            s = values + s + " VNĐ";
            return s;
        }

        protected void ibtnMuangay_Click(object sender, ImageClickEventArgs e)
        {
            DataTable cart;
            DataTable product;
            string id = Request.QueryString["idProduct"];
            if (Session["Cart"] == null)
            {
                cart = new DataTable();
                cart.Columns.Add("ID", typeof (string));
                cart.Columns.Add("Name", typeof (string));
                cart.Columns.Add("Quantity", typeof (string));
                cart.Columns.Add("PriceNew", typeof (string));
                cart.Columns.Add("TotalMoney", typeof (string));
                product = _productService.Product_GetById(id);
                DataRow row = cart.NewRow();
                row["ID"] = id;
                row["Name"] = product.Rows[0]["Name"].ToString();
                row["Quantity"] = 1;
                row["PriceNew"] = product.Rows[0]["PriceNew"].ToString();
                row["TotalMoney"] = (float.Parse(row["PriceNew"].ToString())*Int32.Parse(row["Quantity"].ToString()));
                cart.Rows.Add(row);
            }
            else
            {
                cart = (DataTable) Session["Cart"];
                product = _productService.Product_GetById(id);
                int dem = 0;
                for (int i = 0; i < cart.Rows.Count; i++)
                {
                    if (cart.Rows[i]["ID"].ToString() == id)
                    {
                        dem = int.Parse(cart.Rows[i]["Quantity"].ToString());
                        break;
                    }
                }
                DataRow row = cart.NewRow();
                row["ID"] = id;
                row["Name"] = product.Rows[0]["Name"].ToString();
                if (dem != 0)
                {
                    row["Quantity"] = dem + 1;
                }
                else
                {
                    row["Quantity"] = 1;
                }

                row["PriceNew"] = product.Rows[0]["PriceNew"].ToString();
                row["TotalMoney"] = (float.Parse(row["PriceNew"].ToString())*Int32.Parse(row["Quantity"].ToString()));
                cart.Rows.Add(row);
            }
            Session["Cart"] = cart;
            Response.Redirect("~/TrangChinh/ShoppingCart.aspx?idProduct=" + Request.QueryString["idProduct"]);
        }

        protected void rptImgThums_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string img = e.CommandArgument.ToString();
            if (e.CommandName == "Image")
            {
                imgHinhanh.ImageUrl = img;
            }
        }

     protected void rptSize_ItemCommand1(object source, RepeaterCommandEventArgs e)
        {
            string size = e.CommandArgument.ToString();
            DataTable dt = _sizeService.Size_GetById(Int32.Parse(size));
            if (e.CommandName == "Size")
            {
                lblSize.Text = dt.Rows[0]["Size"].ToString();
                hfSize.Value = size;
            }
        }
    }
}